// 测评模型
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--模型数据-->
      <el-col :span="24" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="量表名称" prop="formName">
            <el-select
              v-model="queryParams.formId"
              placeholder="请选择量表"
              :disabled="displays"
              style="width: 100%"
            >
              <el-option
                v-for="item in answerLiat"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="标 签" prop="degree">
            <el-select
              v-model="queryParams.degree"
              placeholder="请选择"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.sys_label_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:user:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:user:remove']"
              >删除</el-button
            >
          </el-col>
        </el-row>

        <el-table
          v-loading="loading"
          :data="userList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="量表名称"
            align="center"
            key="formName"
            prop="formName"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="模型分值" align="center" width="160">
            <template slot-scope="scope">
              <span>{{ scope.row.startScore }}</span>
              <span>~</span>
              <span>{{ scope.row.endScore }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="标 签"
            align="center"
            prop="degreeDictText"
            width="160"
          />
          <el-table-column
            label="描 述"
            align="center"
            prop="description"
            width="360"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:user:edit']"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:user:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getmodel"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="量表名称" prop="formId">
              <el-select
                v-model="form.formId"
                placeholder="请选择量表"
                :disabled="displays"
                style="width: 100%"
              >
                <el-option
                  v-for="item in answerLiat"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-table :data="elements">
              <el-table-column label="公式说明" align="center">
                <template slot-scope="scope">
                  <el-form-item :label="scope.row.name" prop="formId">
                    <el-input
                      v-model="scope.row.formulaSubd"
                      placeholder="请输入内容"
                      style="width: 100%"
                      :disabled="displays"
                    ></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="预警分值" align="center">
                <template slot-scope="scope">
                  <div
                    class="score"
                    v-for="(item, index) in scope.row.factorList"
                    :key="index"
                  >
                    <el-input
                      v-model="item.startScore"
                      placeholder="请输入内容"
                      style="width: 130px"
                      :disabled="displays"
                    ></el-input>
                    ~
                    <el-input
                      v-model="item.endScore"
                      style="width: 130px"
                      placeholder="请输入内容"
                      :disabled="displays"
                    ></el-input>
                    <el-select
                      v-model="item.degree"
                      placeholder="请选择"
                      clearable
                      style="width: 100px"
                    >
                      <el-option
                        v-for="dict in dict.type.sys_label_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      />
                    </el-select>
                    <div class="stageicon">
                      <div>
                        <i
                          class="el-icon-circle-plus-outline"
                          font-size="16px"
                          @click="plus('factor', scope.row)"
                        ></i>
                      </div>
                      <div v-if="index !== 0">
                        <i
                          class="el-icon-remove-outline"
                          @click="minus('factor', scope.row, index)"
                        ></i>
                      </div>
                    </div>
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                label="描述"
                align="center"
                prop="description"
                width="260"
              >
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.description"
                    type="textarea"
                    :rows="5"
                    placeholder="请输入内容"
                    :disabled="displays"
                  ></el-input>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  formcfgModel, //添加模型
  putModel, //修改模型
  fmodelDel, //删除模型
  formcfgList, //量表列表
  modelPage, //模型列表
} from "@/api/itemBank/index";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  evaluationModel, //模型必填效验
} from "@/utils/verification";

export default {
  dicts: [
    "sys_normal_disable",
    "sys_label_type",
    "suitable_object",
    "yes_no_switch",
  ],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,

      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "",
      // 类型选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 默认密码
      initPassword: undefined,
      // 日期范围
      dateRange: [],

      // 量表
      answerLiat: [],

      displays: false, //禁止输入
      elements: [
        {
          formulaType: 1,
          name: "因子计算",
          factorList: [
            {
              startScore: "",
              endScore: "",
              degree: "",
            },
          ],
        },
        {
          formulaType: 2,
          name: "总分计算",
          factorList: [
            {
              startScore: "",
              endScore: "",
              degree: "",
            },
          ],
        },
      ], //模型设置
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/user/importData",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },

      // 表单校验
      rules: {
        formId: [{ required: true, message: "量表不能为空", trigger: "blur" }],
      },
    };
  },
  watch: {},
  created() {
    this.getList();
    this.getmodel();
  },
  methods: {
    // 添加分值
    plus(type, row) {
      let date = {
        startScore: "",
        endScore: "",
        degree: "",
      };
      if (type == "factor") {
        row.factorList.push(date);
      } else {
        console.log("传参", type, row);
      }
    },
    // 删减分值
    minus(type, row, index) {
      if (type == "factor") {
        row.factorList.splice(
          row.factorList.findIndex((item) => item === index),
          1
        );
      } else {
        console.log("传参", type, row);
      }
    },
    /** 查询量表列表 */
    getList() {
      this.loading = true;
      formcfgList({ pageSize: 999, examType: 1 }).then((response) => {
        this.answerLiat = response.rows;
      });
    },
    getmodel() {
      this.loading = true;
      modelPage(this.queryParams).then((res) => {
        this.userList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        
      };
      this.elements = [
        {
          formulaType: 1,
          name: "因子计算",
          factorList: [
            {
              startScore: "",
              endScore: "",
              degree: "",
            },
          ],
        },
        {
          formulaType: 2,
          name: "总分计算",
          factorList: [
            {
              startScore: "",
              endScore: "",
              degree: "",
            },
          ],
        },
      ];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log("查询", this.dateRange);

      this.queryParams.pageNum = 1;
      this.getmodel();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模型";
      this.displays = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      // this.elements=[]
      this.reset();
      this.title = "编辑模型";
      this.displays = false;
      this.form = row;
      this.elements[0] = this.form;
      this.elements[0].index = 1;
      this.open = true;
    },

    /** 提交按钮 */
    submitForm: function () {
      this.form = { formId: this.form.formId, ...this.elements };
      console.log("提交", this.form,this.elements);
      return;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            putModel(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getmodel();
            });
          } else {
            if (evaluationModel(this.elements) !== "") {
              this.$modal.msgError(evaluationModel(this.elements));
            } else {
              formcfgModel(this.form).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getmodel();
              });
            }
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除编号为"' + userIds + '"的数据项？')
        .then(function () {
          return fmodelDel(userIds);
        })
        .then(() => {
          this.getmodel();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped lang="scss">
.subject {
  width: 100%;
  margin-top: -30px;
  margin-bottom: 10px;
  font-size: 18px;
  padding-left: 5px;
}
.score {
  display: flex;
  //  margin: 0 auto;
  padding: 0 60px;
  margin-bottom: 5px;
  .stageicon {
    margin-left: 5px;
    font-size: 18px;
  }
}
</style>

